package com.lzh.rocblogbackend.controller;

import com.lzh.rocblogbackend.common.AjaxResult;
import com.lzh.rocblogbackend.mapper.CommentMapper;
import com.lzh.rocblogbackend.mapper.ReplyMapper;
import com.lzh.rocblogbackend.model.Reply;
import com.lzh.rocblogbackend.model.Reply;
import com.lzh.rocblogbackend.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

@RestController
public class ReplyController {
    @Autowired
    ReplyMapper replyMapper;
    @RequestMapping("/upload/reply")
    public HashMap<String,Object> uploadComment(String content, String email, String qq, int commentId) {
        //获取QQ头像
        String headshot="http://q1.qlogo.cn/g?b=qq&nk="+qq+"&s=100";
        //获取QQ昵称
        RestTemplate restTemplate=new RestTemplate();
        String accessToken = "fakeAT";
        String nickname = restTemplate.getForEntity("https://api.kuizuo.cn/api/qqnick?qq="+qq,
                String.class,accessToken).getBody();
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setEmail(email);
        reply.setHeadshot(headshot);
        reply.setNickname(nickname);
        reply.setCommentId(commentId);
        reply.setTime(TimeUtil.getCurrentTime());
        int ret = replyMapper.upload(reply);
        if(ret==1) {
            return AjaxResult.success("评论成功",null);
        } else {
            return AjaxResult.fail(-1,"评论失败");
        }
    }
}
